package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.inter.IDownloadRequestListenter;
import com.hzdesk.media.koreabatchmatvector.logic.contract.AboutContract;
import com.hzdesk.media.koreabatchmatvector.request.NetworkRequest;
import com.hzdesk.media.koreabatchmatvector.utils.DeleteDirTest;

import java.io.File;

/**
 * Created by Administrator on 2018/7/10.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class Aboutpersenter implements AboutContract.IAboutpersenter {

    private AboutContract.IAboutView mIAboutView;

    private AboutContract.IAboutModle mIAboutModle;
    private String dwonloadPath;
    private Context mContext;
    private String dwonloadPath1;

    public Aboutpersenter(AboutContract.IAboutView tIAboutView) {
        this.mIAboutView = tIAboutView;
    }

    @Override
    public void oncliackBack() {
        mIAboutView.toFishPager();
    }

    @Override
    public void onclickedServiceAgreement() {
        mIAboutView.toServiceAgreementPager();
    }

    @Override
    public void onclickedCopyrightInfo() {
        mIAboutView.toCopyrightInfoPager();
    }

    // todo 下载更新版本
    @Override
    public void onckliacdeVersionUpdate(Context tContext) {
        mContext = tContext;
        String version = getAppVersionName(mContext);
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        String newVersionName = mSharedPreferences.getString("software_version", version);
        if (!newVersionName.equals(version)) {
            String versionUrl = mSharedPreferences.getString("software_version_new_url", "");
            // todo 下载apk 替换原来的apk
            NetworkRequest downloadVersion = new NetworkRequest();
            downloadVersion.setmIDownloadRequestListenter(mIDownloadRequestListenter);
            boolean path = DeleteDirTest.crteateSdCrad("Dwonload");
            dwonloadPath1 = DeleteDirTest.getPath("Dwonload") + "/" + DeleteDirTest.getDownloadFileName(versionUrl);
            Log.e("Aboutpersenter", "onckliacdeVersionUpdate: "+versionUrl );
            if(versionUrl != null && !TextUtils.isEmpty(versionUrl)){
                downloadVersion.DownLoadFile(dwonloadPath1, DeleteDirTest.getDownloadFileName(versionUrl), versionUrl);
            }
        } else {
            mIAboutView.hidenPromptInfo();

        }
    }

    @Override
    public void oncklicedAboutDeskMedia() {
        mIAboutView.toAboutDeskMediaPager();
    }

    @Override
    public void setListener(LinearLayout linearLayout) {
        linearLayout.setOnHoverListener(onHoverListener);
        linearLayout.setOnFocusChangeListener(onFocusChangeListener);
    }

    //鼠标移动事件
    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    //v.clearFocus();
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };
    //焦点改变事件
    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LinearLayout linearLayout = (LinearLayout) v;
            if (hasFocus) {
                linearLayout.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.02f);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.02f);
                ObjectAnimator translationZ = ObjectAnimator.ofFloat(v, "translationZ", 0f, 2.0f);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            } else {
                linearLayout.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01 = ObjectAnimator.ofFloat(v, "scaleX", 1.02f, 1.0f);
                ObjectAnimator scaleY_01 = ObjectAnimator.ofFloat(v, "scaleY", 1.02f, 1.0f);
                ObjectAnimator translationZ_01 = ObjectAnimator.ofFloat(v, "translationZ", 2.0f, 0f);
                AnimatorSet animatorSet_01 = new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };

    /**
     * 返回当前程序版本名
     */
    public String getAppVersionName(Context mContext) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = mContext.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    private IDownloadRequestListenter mIDownloadRequestListenter = new IDownloadRequestListenter() {

        @Override
        public void success(File response, int id) {
            mIAboutView.hidenPromptInfo();
            // todo 更新替换apk
            installApk();
            mIAboutView.showToast("替换成功!");
        }

        @Override
        public void fail(String s) {

        }

        @Override
        public void Progress(float progress) {

        }
    };

    // 安装Apk
    private void installApk() {
        try {
            Intent i = new Intent(Intent.ACTION_VIEW);
          //  String filePath = DownloadManagerUtils.APP_FILE_NAME;
            i.setDataAndType(Uri.parse("file://" + dwonloadPath1), "application/vnd.android" + ".package-archive");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(i);
        } catch (Exception e) {
            Log.e("VersionInfo", "安装失败");
        }
    }


}
